package com.hangzhou.xyj.ui.adapter;

import android.app.Dialog;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.db.GroupPushMsgDao;
import com.hangzhou.xyj.model.GroupPushMsgModel;
import com.hangzhou.xyj.ui.widget.CircleImageView;
import com.hangzhou.xyj.ui.widget.SelectMenuDialog;
import com.hangzhou.xyj.ui.widget.multiimageselector.utils.TimeUtils;
import com.hangzhou.xyj.ui.widget.views.MyTextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yaohuix on 2016/6/7  18:56
 * <p/>
 * 说明：
 */
public class MyOfficalMessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final String TAG = "MyOfficalMessageAdapter";
    private Context mContext;
    private LayoutInflater inflater;
    private List<GroupPushMsgModel> mDatas;
    private GroupPushMsgDao mDao;
    private boolean isFromNotification;

    public MyOfficalMessageAdapter(Context context, List<GroupPushMsgModel> mDatas) {
        this.mContext = context;
        this.mDatas = mDatas;
        this.inflater = LayoutInflater.from(context);
        this.mDao = new GroupPushMsgDao(context);
    }

    public void setFromNotification(boolean isFromNotification) {
        this.isFromNotification = isFromNotification;
    }

    public void update(List<GroupPushMsgModel> list) {
        if (list == null || list.size() == 0) {
            mDatas = new ArrayList<>();
        }
        mDatas = list;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.layout_offical_message_item_content, parent, false);
        return new MessageItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        ((MessageItemViewHolder) holder).setData(mDatas.get(position));
        if (!isFromNotification) {
            ((MessageItemViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SelectMenuDialog menuDialog = new SelectMenuDialog(mContext);
                    String[] menu = new String[]{"删除"};
                    menuDialog.setMenu(menu, new SelectMenuDialog.SelectMenuClickListener() {
                        @Override
                        public void onSelectMenuClick(Dialog dialog, View item, int innerPosition) {
                            dialog.dismiss();
                            mDao.delete(mDatas.get(position).getId());
                            mDatas.remove(holder.getAdapterPosition());
                            notifyItemRemoved(holder.getAdapterPosition());
                        }
                    });
                    menuDialog.show();
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        if (mDatas == null)
            return 0;
        return mDatas.size();
    }

    /**
     * 消息的ViewHolder类
     */
    class MessageItemViewHolder extends RecyclerView.ViewHolder {
        CircleImageView mImageView;//头像Image
        MyTextView mTvName;//昵称
        MyTextView mTvMessage;//消息
        MyTextView mTvTime;//时间
        MyTextView mTvUnReadCount;//未读数

        public MessageItemViewHolder(View itemView) {
            super(itemView);
            mImageView = (CircleImageView) itemView.findViewById(R.id.miv_message_header);
            mTvName = (MyTextView) itemView.findViewById(R.id.mtv_message_name);
            mTvMessage = (MyTextView) itemView.findViewById(R.id.mtv_message_message);
            mTvTime = (MyTextView) itemView.findViewById(R.id.mtv_message_time);
            mTvUnReadCount = (MyTextView) itemView.findViewById(R.id.tv_un_read_count);
        }


        private void setData(GroupPushMsgModel model) {
            mTvTime.setText(TimeUtils.timeFormat(model.getMsgTime(), "MM-dd HH:mm:ss"));
            mTvName.setText("戏友记官方");
            mImageView.setImageResource(R.drawable.ic_offical_head);
            mTvMessage.setText(model.getContent());
        }
    }

}
